﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNA_TD.Menue
{
    public abstract class GUI : IRunable, IUpdate, IDraw
    {
        protected Rectangle size;
        protected bool mouseInside { get; private set; }

        public GUI()
            : base(Priority.GUI)
        {
            mouseInside = false;
            Register(this);
        }

        public virtual void Update()
        {
            if (MouseInside(size))
            {
                if (buttonPressed.MouseLeftButtonPressed())
                {
                    MouseClick();
                }
                else if (mouseInside == false)
                {
                    mouseInside = true;
                    MouseEnter();
                }
            }
            else if (mouseInside == true)
            {
                mouseInside = false;
                MouseLeave();
            }
        }

        public void SelectTowerToUpgrade()
        {
            var ray = GetMouseRay();
            foreach(var towerToTest in gameManager.getRenderList(Priority.Tower))
            {
                var isTower = towerToTest as Tower;
                if (isTower != null) // && towerToTest as Base == null)
                {
                    if (ray.Intersects(isTower.GetBoundingSphere()).HasValue)
                    {
                        new UpgradeMenue(isTower);
                        UnRegister(this);
                        return;
                    }
                }
            }
        }

        public static Ray GetMouseRay()
        {
            Vector2 mousePosition = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

            Vector3 nearPoint = new Vector3(mousePosition, 0);
            Vector3 farPoint = new Vector3(mousePosition, 1);
            nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearPoint, projectionMatrix, viewMatrix, Matrix.Identity);
            farPoint = graphics.GraphicsDevice.Viewport.Unproject(farPoint, projectionMatrix, viewMatrix, Matrix.Identity);
            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();
            return new Ray(nearPoint, direction);
        }

        public static bool MouseInside(Rectangle rect)
        {
            var mouseState = Mouse.GetState();
            if (mouseState.X > rect.X &&
                mouseState.X < rect.X + rect.Width &&
                mouseState.Y > rect.Y &&
                mouseState.Y < rect.Y + rect.Height)
                return true;
            return false;
        }

        public abstract void Draw();
        public abstract void Resize();
        public abstract void MouseClick();
        public virtual void MouseEnter() { }
        public virtual void MouseLeave() { }

        public override void Load()
        {
        }

        public override void UnLoad()
        {
        }
    }
}
